﻿///// AnnotatedChunks /////

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace AnnotatedChunks
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creates a PDF file with annotations.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Annotations");
		    Console.WriteLine("-> Creates a PDF file with annotations;");
		    Console.WriteLine("-> files generated:");
		    Console.WriteLine("   annotated_chunks.pdf");
		    // step 1: creation of a document-object
		    Document document = new Document();
		    try 
            { 
			    // step 2:
			    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("annotated_chunks.pdf", FileMode.Create));
			    // step 3:
			    document.Open();
			    // step 4:
			    PdfAnnotation text = PdfAnnotation.CreateText(writer, new Rectangle(200f, 250f, 300f, 350f), "Fox", "The fox is quick", true, "Comment");
			    PdfAnnotation attachment = PdfAnnotation.CreateFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "Image of the fox and the dog", getBytesFromFile(@"C:\Sample Code\iText\PDFs\foxdog.jpg"), null, "foxdog.jpg");
			    PdfAnnotation javascript = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.JavaScript("app.alert('Wake up dog!');\r", writer));
			    Chunk fox = new Chunk("quick brown fox").SetAnnotation(text);
			    Chunk jumps = new Chunk(" jumps over ").SetAnnotation(attachment);
			    Chunk dog = new Chunk("the lazy dog").SetAnnotation(javascript);
			    document.Add(fox);
			    document.Add(jumps);
			    document.Add(dog);
		    } 
            
            catch (Exception de) 
            {
			    Console.Error.WriteLine(de.Message);
		    }
 
		    // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }

        private static byte[] getBytesFromFile(string fileName) 
        {
		    FileStream ifs = new FileStream(fileName, FileMode.Open);
		    long length = ifs.Length;
		    byte[] bytes = new byte[(int) length];
		    int offset = 0;
		    int numRead = 0;

		    while (offset < bytes.Length && (numRead = ifs.Read(bytes, offset, bytes.Length - offset)) >= 0) 
            {
			    offset += numRead;
		    }

		    if (offset < bytes.Length) 
                {
                    Console.WriteLine("Could not completely read file " + ifs.Name);
		        }

		    ifs.Close();
		    return bytes;
	    }
    }
}

///// AnnotatedImages /////

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace AnnotatedImages
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creates a PDF file with an annotated image.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example AnnotatedImages");
		    Console.WriteLine("-> Creates a PDF with annotated images;");
		    Console.WriteLine("-> resources needed: foxdog.jpg (chapter 5) and iTextLogo.gif (chapter 10)");
		    Console.WriteLine("-> file generated:");
		    Console.WriteLine("   annotated_images.pdf");
		    // step 1: creation of a document-object
		    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		    try 
            {
			    // step 2:
			    // we create a writer that listens to the document
                PdfWriter.GetInstance(document, new FileStream("annotated_images.pdf", FileMode.Create));
			    // step 3: we open the document
			    document.Open();
			    // step 4: we add some content
			    Image gif = Image.GetInstance(@"C:\Sample Code\iText\PDFs\iTextLogo.gif");
			    gif.Annotation = new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText");
			    gif.SetAbsolutePosition(30f, 750f);
			    document.Add(gif);
                Image jpeg = Image.GetInstance(@"C:\Sample Code\iText\PDFs\foxdog.jpg");
			    jpeg.Annotation = new Annotation("picture", "quick brown fox jumps over the lazy dog", 0, 0, 0, 0);
			    jpeg.SetAbsolutePosition(120f, 550f);
			    document.Add(jpeg);
		    } 
            
            catch (Exception de)    
            {
			    Console.Error.WriteLine(de.Message);
		    }
 
		    // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }
    }
}

///// Annotations /////

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Annotations
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creates a PDF file with annotations.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Annotations");
		    Console.WriteLine("-> Creates a PDF file with annotations;");
		    Console.WriteLine("-> resources needed: foxdog.jpg (chapter 5) and foxdog.mpg");
		    Console.WriteLine("-> files generated:");
		    Console.WriteLine("   annotations.pdf");
		    // step 1: creation of a document-object
		    Document document = new Document();
		    try 
            { 
			    // step 2:
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("annotations.pdf", FileMode.Create));
			    // step 3:
			    writer.PdfVersion = PdfWriter.VERSION_1_5;
			    document.Open();
			    // step 4:
			    document.Add(new Chunk("top of the page").SetLocalDestination("top"));
			    PdfContentByte cb = writer.DirectContent;
			    // page 1
			    PdfAnnotation annotation = new PdfAnnotation(writer, new Rectangle(100, 750, 150, 800));
			    writer.AddAnnotation(annotation);
			    annotation.Put(PdfName.SUBTYPE, PdfName.TEXT);
			    annotation.Put(PdfName.OPEN, PdfBoolean.PDFTRUE);
			    annotation.Put(PdfName.T, new PdfString("custom"));
			    annotation.Put(PdfName.CONTENTS, new PdfString("This is a custom built text annotation."));
			    cb.Rectangle(100, 750, 50, 50);
			    cb.Stroke();
     
			    PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(writer, @"C:\Sample Code\iText\PDFs\foxdog.mpg", "foxdog.mpg", null);
			    writer.AddAnnotation(PdfAnnotation.CreateScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "Fox and Dog", fs, "video/mpeg", true));
			    PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.JavaScript("app.alert('Hello');\r", writer));
			    writer.AddAnnotation(a);
                string str = "some text";
                ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = encoding.GetBytes(str);
			    writer.AddAnnotation(PdfAnnotation.CreateFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", byteArray, null, "some.txt"));
			    writer.AddAnnotation(PdfAnnotation.CreateText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help"));
			    writer.AddAnnotation(PdfAnnotation.CreateText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment"));
			    cb.Rectangle(200, 700, 100, 100);
			    cb.Rectangle(200, 550, 100, 100);
			    cb.Rectangle(200, 400, 100, 100);
			    cb.Rectangle(200, 250, 100, 100);
			    cb.Stroke();
			    document.NewPage();
			    // page 2
			    writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_INVERT, PdfAction.JavaScript("app.alert('Hello');\r", writer)));
			    writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
			    writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(400f, 700f, 500f, 800f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
			    writer.AddAnnotation(PdfAnnotation.CreatePopup(writer, new Rectangle(400f, 550f, 500f, 650f), "Hello, I'm a popup!", true));
     
			    PdfAnnotation shape1 = PdfAnnotation.CreateSquareCircle(writer, new Rectangle(200f, 400f, 300f, 500f), "This Comment annotation was made with 'createSquareCircle'", false);
			    float[] red = { 1, 0, 0 };
			    shape1.Put(new PdfName("IC"), new PdfArray(red));
			    writer.AddAnnotation(shape1);
			    PdfAnnotation shape2 = PdfAnnotation.CreateLine(writer, new Rectangle(200f, 250f, 300f, 350f), "this is a line", 200, 250, 300, 350);
			    shape2.Color = BaseColor.BLUE;
			    PdfArray lineEndingStyles = new PdfArray();
			    lineEndingStyles.Add(new PdfName("Diamond"));
			    lineEndingStyles.Add(new PdfName("OpenArrow"));
			    shape2.Put(new PdfName("LE"), lineEndingStyles);
			    shape2.Put(PdfName.BS, new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
			    writer.AddAnnotation(shape2);
			    PdfContentByte pcb = new PdfContentByte(writer);
			    pcb.SetColorFill(BaseColor.RED);
			    PdfAnnotation freeText = PdfAnnotation.CreateFreeText(writer, new Rectangle(400f, 400f, 500f, 500f), "This is some free text, blah blah blah", pcb);
			    writer.AddAnnotation(freeText);
			    PdfAnnotation attachment = PdfAnnotation.CreateFileAttachment(writer, new Rectangle(400f, 250f, 500f, 350f), "Image of the fox and the dog", null, @"C:\Sample Code\iText\PDFs\foxdog.jpg", "foxdog.jpg");
			    attachment.Put(PdfName.NAME, new PdfString("Paperclip"));
			    writer.AddAnnotation(attachment); 
		    } 
            
            catch (Exception de) 
            {
			    Console.Error.WriteLine(de.Message);
		    }
 
		    // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }
    }
}

///// Buttons /////

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Buttons
{
    class Program
    {
        static void Main(string[] args)
        {
            // Demonstrates the different types of buttons.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Buttons");
		    Console.WriteLine("-> Creates a PDF file with widget annotations of type button;");
		    Console.WriteLine("-> files generated:");
		    Console.WriteLine("   buttons.pdf");
 
		    // step 1: creation of a document-object
		    Document document = new Document();
		    try 
            {
 			    // step 2:
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("buttons.pdf", FileMode.Create));
			    // step 3:
			    document.Open();
			    writer.AddJavaScript("function showButtonState() {\n"
					    + "app.alert('Checkboxes:"
					    + " English: ' + this.getField('English').value + "
					    + "' French: ' + this.getField('French').value + "
					    + "' Dutch: ' + this.getField('Dutch').value + "
					    + "' Radioboxes: ' + this.getField('language').value);"
					    + "\n}");
			    // step 4:
			    PdfContentByte cb = writer.DirectContent;
			    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
			    String[] languages = { "English", "French", "Dutch" };
			    Rectangle rect;
     
			    PdfAppearance[] radiobuttonStates = new PdfAppearance[2];
			    radiobuttonStates[0] = cb.CreateAppearance(20, 20);
			    radiobuttonStates[0].Circle(10, 10, 9);
			    radiobuttonStates[0].Stroke();
			    radiobuttonStates[1] = cb.CreateAppearance(20, 20);
			    radiobuttonStates[1].Circle(10, 10, 9);
			    radiobuttonStates[1].Stroke();
			    radiobuttonStates[1].Circle(10, 10, 3);
			    radiobuttonStates[1].FillStroke();
     
			    PdfFormField language = PdfFormField.CreateRadioButton(writer, true);
			    language.FieldName = "language";
			    language.ValueAsName = languages[0];

			    for (int i = 0; i < languages.Length; i++) 
                {
				    rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
				    addRadioButton(writer, rect, language, languages[i], radiobuttonStates, i == 0);
				    cb.BeginText();
				    cb.SetFontAndSize(bf, 18);
				    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
				    cb.EndText();
			    }

			    writer.AddAnnotation(language);
			    PdfAppearance[] checkboxStates = new PdfAppearance[2];
			    checkboxStates[0] = cb.CreateAppearance(20, 20);
			    checkboxStates[0].Rectangle(1, 1, 18, 18);
			    checkboxStates[0].Stroke();
			    checkboxStates[1] = cb.CreateAppearance(20, 20);
			    checkboxStates[1].SetRGBColorFill(255, 128, 128);
			    checkboxStates[1].Rectangle(1, 1, 18, 18);
			    checkboxStates[1].FillStroke();
			    checkboxStates[1].MoveTo(1, 1);
			    checkboxStates[1].LineTo(19, 19);
			    checkboxStates[1].MoveTo(1, 19);
			    checkboxStates[1].LineTo(19, 1);
			    checkboxStates[1].Stroke();
     
			    for (int i = 0; i < languages.Length; i++) 
                {
				    rect = new Rectangle(260, 806 - i * 40, 280, 788 - i * 40);
				    createCheckbox(writer, rect, languages[i], checkboxStates);
				    cb.BeginText();
				    cb.SetFontAndSize(bf, 18);
				    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 290, 790 - i * 40, 0);
				    cb.EndText();
			    }
     
			    PdfAppearance normal = cb.CreateAppearance(100, 50);
			    normal.SetColorFill(BaseColor.GRAY);
			    normal.Rectangle(5, 5, 90, 40);
			    normal.Fill();
			    PdfAppearance rollover = cb.CreateAppearance(100, 50);
			    rollover.SetColorFill(BaseColor.RED);
			    rollover.Rectangle(5, 5, 90, 40);
			    rollover.Fill();
			    PdfAppearance down = cb.CreateAppearance(100, 50);
			    down.SetColorFill(BaseColor.BLUE);
			    down.Rectangle(5, 5, 90, 40);
			    down.Fill();
			    PdfFormField pushbutton = PdfFormField.CreatePushButton(writer);
			    pushbutton.FieldName = "PushAction";
			    pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
			    pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
			    pushbutton.SetAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
			    pushbutton.SetWidget(new Rectangle(40, 650, 150, 680), PdfAnnotation.HIGHLIGHT_PUSH);
			    pushbutton.Action = PdfAction.JavaScript("this.showButtonState()", writer);
			    writer.AddAnnotation(pushbutton);
		    } 
            
            catch (Exception e) 
            {
			    Console.Error.WriteLine(e.Message);
		    }
		
            // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }

        private static void addRadioButton(PdfWriter writer, Rectangle rect, PdfFormField radio, String name, PdfAppearance[] onOff, bool on)
        {
            PdfFormField field = PdfFormField.CreateEmpty(writer);
            field.SetWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT);

            if (on)
                field.AppearanceState = name;
            else
                field.AppearanceState = "Off";

            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, name, onOff[1]);
            radio.AddKid(field);
        }

        private static void createCheckbox(PdfWriter writer, Rectangle rect, String name, PdfAppearance[] onOff)
        {
            PdfFormField field = PdfFormField.CreateCheckBox(writer);
            field.SetWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT);
            field.FieldName = name;
            field.ValueAsName = "Off";
            field.AppearanceState = "Off";
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);
            writer.AddAnnotation(field);
        }
    }
}

///// Buttons2 /////

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Buttons2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Demonstrates the different types of buttons.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Buttons2");
		    Console.WriteLine("-> Creates a PDF file with widget annotations of type button;");
		    Console.WriteLine("-> files generated:");
		    Console.WriteLine("   buttons2.pdf");
 
		    // step 1: creation of a document-object
		    Document document = new Document();
		    try 
            { 
			    // step 2:
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("buttons2.pdf", FileMode.Create));
			    // step 3:
			    document.Open();
			    writer.AddJavaScript("function showButtonState() {\n"
					    + "app.alert('Checkboxes:"
					    + " English: ' + this.getField('English').value + "
					    + "' French: ' + this.getField('French').value + "
					    + "' Dutch: ' + this.getField('Dutch').value + "
					    + "' Radioboxes: ' + this.getField('language').value);"
					    + "\n}");
			    // step 4:
			    PdfContentByte cb = writer.DirectContent;
			    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
			    String[] languages = { "English", "French", "Dutch" };
			    Rectangle rect;
     
			    PdfFormField language = PdfFormField.CreateRadioButton(writer, true);
			    language.FieldName = "language";
			    language.ValueAsName = languages[0];

			    for (int i = 0; i < languages.Length; i++) 
                {
				    rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
				    addRadioButton(writer, rect, language, languages[i], i == 0);
				    cb.BeginText();
				    cb.SetFontAndSize(bf, 18);
				    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
				    cb.EndText();
			    }

			    writer.AddAnnotation(language);
     
			    for (int i = 0; i < languages.Length; i++) 
                {
				    rect = new Rectangle(260, 806 - i * 40, 280, 788 - i * 40);
				    createCheckbox(writer, rect, languages[i]);
				    cb.BeginText();
				    cb.SetFontAndSize(bf, 18);
				    cb.ShowTextAligned(Element.ALIGN_LEFT, languages[i], 290, 790 - i * 40, 0);
				    cb.EndText();
			    }

			    PdfTemplate template = cb.CreateTemplate(150, 150);
			    template.SetLineWidth(12f);
			    template.Arc(40f - (float) Math.Sqrt(12800), 110f + (float) Math.Sqrt(12800), 200f - (float) Math.Sqrt(12800), -50f + (float) Math.Sqrt(12800), 281.25f, 33.75f);
			    template.Arc(40f, 110f, 200f, -50f, 90f, 45f);
			    template.Stroke();
			    template.SetLineCap(PdfContentByte.LINE_JOIN_ROUND);
			    template.Arc(80f, 30f, 160f, 110f, 90f, 180f);
			    template.Arc(115f, 65f, 125f, 75f, 0f, 360f);
			    template.Stroke();
     
			    PushbuttonField push = new PushbuttonField(writer, new Rectangle(40, 650, 150, 680), "pushAction");
			    push.BackgroundColor = BaseColor.YELLOW;
			    push.BorderColor = BaseColor.BLACK;
			    push.Text = "Push";
			    push.TextColor = BaseColor.RED;
			    push.Template = template;
			    push.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
			    push.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
			    PdfFormField pushbutton = push.Field;
			    pushbutton.Action = PdfAction.JavaScript("this.ShowButtonState()", writer);
			    writer.AddAnnotation(pushbutton);
		    } 
            
            catch (Exception e) 
            {
			    Console.Error.WriteLine(e.Message);
		    }

		    // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }

        private static void addRadioButton(PdfWriter writer, Rectangle rect, PdfFormField radio, String name, bool on)
		{
		    RadioCheckField check = new RadioCheckField(writer, rect, null, name);
		    check.CheckType = RadioCheckField.TYPE_STAR;
		    check.Checked = on;
		    radio.AddKid(check.RadioField);
	    }

        private static void createCheckbox(PdfWriter writer, Rectangle rect, String name)  
        {
		    RadioCheckField check = new RadioCheckField(writer, rect, name, "On");
		    check.CheckType = RadioCheckField.TYPE_CROSS;
		    writer.AddAnnotation(check.CheckField);
	    }
    }
}

///// This page is still under construction /////